﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Wx.Sdk.MiniProgram;
using Wx.Sdk.MiniProgram.Options;
using Wx.Sdk.Cache;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class MiniProgramServiceCollectionExtensions
    {
        /// <summary>
        /// 小程序注入
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IServiceCollection AddWxMiniProgram(this IServiceCollection services, Action<MiniProgramOption> action)
        {
            var opt = new MiniProgramOption();
            action.Invoke(opt);
            services.AddSingleton(opt);
            services.AddHttpClient();
            services.TryAddSingleton<WxMiniProgram>();
            services.TryAddSingleton<IWxCache, DefaultWxCache>();
            return services;
        }
        /// <summary>
        /// 小程序注入 需要实现IWxCache
        /// </summary>
        /// <typeparam name="TCache">IWxCache</typeparam>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddWxMiniProgram<TCache>(this IServiceCollection services, Action<MiniProgramOption> action) where TCache : class, IWxCache
        {
            services.AddWxMiniProgram(action);
            services.RemoveAll<IWxCache>();
            services.TryAddSingleton<IWxCache, TCache>();
            return services;
        }
    }
}
